package com.showcai.controller.product;

import com.github.pagehelper.PageInfo;
import com.showcai.controller.base.BaseController;
import com.showcai.def.Constant;
import com.showcai.module.Organization;
import com.showcai.module.ProductEntity;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.OrganizationService;
import com.showcai.service.ProductEntityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-3.
 */
@RequestMapping("/productEntity")
@Controller
public class ProductEntityContorller extends BaseController {

    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    OrganizationService organizationService;

    @RequestMapping(value = "/productEntityList")
    public ModelAndView productEntityList(Long productId, Integer page, Integer pageSize) {

        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/product/productEntityList");
        Map<String, String> map = this.showRequestParameters();
        if (productId != null) {
            map.put("productId", String.valueOf(productId));
        } else {
            throw new MyRunTimeExcption("没找到产品ID！");
        }

        map.put("page", String.valueOf(page));
        map.put("pageSize", String.valueOf(pageSize));
        PageInfo pageInfo = productEntityService.getProductEntityPageList(map);
        mov.addObject("productId", productId);
        mov.addObject("pageInfo", pageInfo);
        return mov;
    }


    @RequestMapping(value = "/productEntity")
    public ModelAndView productEntity(Long productEntityId, Long productId, Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/product/productEntity");
        mov.addObject("productId", productId);
        if (productEntityId != null) {
            ProductEntity productEntity = productEntityService.getProductEntityById(productEntityId);
            mov.addObject("productEntity", productEntity);
        }
        List<Organization> list = organizationService.organizationList(new HashMap<>());
        mov.addObject("organizationList", list);
        mov.addObject("productId", productId);
        return mov;
    }


    @RequestMapping(value = "/modifyProductEntity", method = RequestMethod.POST)
    @ResponseBody
    public String modifyProduct(ProductEntity productEntity, @RequestParam(value = "file", required = false) MultipartFile file) {
        ResultObj resultObj = new ResultObj();
        String filePath = "showcai/product/";
        try {
            if (file != null) {
                String photos = super.upLoadFile(file.getInputStream(), filePath);
                productEntity.setPicPath(photos);
                if (StringUtils.isNotBlank(photos)) {
                    productEntity.setPicPath(photos);
                }
            }

            productEntityService.modifyProductEntity(productEntity);
            resultObj.setCode(ReturnCode.SUCCESS.getCode());
            resultObj.setMsg("操作成功");
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();


    }


    @RequestMapping(value = "/stopProductEntity", method = RequestMethod.POST)
    @ResponseBody
    public String stopProductEntity(Long tid) {
        ResultObj resultObj = new ResultObj();
        productEntityService.stopProductEntity(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }

    @RequestMapping(value = "/operateProductEntity", method = RequestMethod.POST)
    @ResponseBody
    public String operateProductEntity(Long tid) {
        ResultObj resultObj = new ResultObj();
        productEntityService.operateProductEntity(tid);
        resultObj.setCode(ReturnCode.SUCCESS.getCode());
        resultObj.setMsg(ReturnCode.SUCCESS.getMsg());
        return resultObj.toJson();
    }


}
